package com.bringway.p2p.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.bringway.framework.controller.JsonController;
import com.bringway.framework.util.RequestUtils;
import com.bringway.p2p.service.P2pWdzrService;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("wdzr")
public class P2pWdzrController extends JsonController {
	@Autowired
	private P2pWdzrService p2pWdzrService;

	@RequestMapping("/queryWdzrSqList")
	public ModelAndView queryWdzrSqList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		JSONObject result = new JSONObject();
		JSONObject params = new JSONObject();

		Integer start = RequestUtils.getIntParameter(request, "start");
		Integer limit = RequestUtils.getIntParameter(request, "limit");
		Integer page = RequestUtils.getIntParameter(request, "page");//新版本,传参
		String startDate = RequestUtils.getStringParameter(request, "startDate");
		String endDate = RequestUtils.getStringParameter(request, "endDate");
		String czrr = RequestUtils.getStringParameter(request, "czrr");
		String cxmid = RequestUtils.getStringParameter(request, "cxmid");
		String czrzt = RequestUtils.getStringParameter(request, "czrzt");
		if (null!=page) {//新版本,传参
			start=(page-1)*limit;
		}
		params.put("start", start);
		params.put("limit", limit);
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		params.put("czrr", czrr);
		params.put("cxmid", cxmid);
		params.put("czrzt", czrzt);
		List<Map<String, Object>> list = p2pWdzrService.queryWdzrSqList(params);
		int count = p2pWdzrService.getWdzrSqCount(params);
		result.put("data", list);
		result.put("total", count);
		if (null!=page) {//新版本,传参
			int totalPage=count%limit==0?count/limit:count/limit+1;
			result.put("totalPage", totalPage);
		}
		serialize(request, response, result);
		return null;
	}

	@RequestMapping("/saveWdzrSh")
	public ModelAndView saveWdzrSh(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String wdzrid=RequestUtils.getStringParameter(request, "wdzrid");
		String tzrxxid=RequestUtils.getStringParameter(request, "tzrxxid");
		String zrly=RequestUtils.getStringParameter(request, "zrly");
		String shr=RequestUtils.getStringParameter(request, "shr");
		String shzt=RequestUtils.getStringParameter(request, "shzt");
		String shbz=RequestUtils.getStringParameter(request, "shbz");
		String mjq=RequestUtils.getStringParameter(request, "mjq");
		String sxfbl=RequestUtils.getStringParameter(request, "sxfbl");
		JSONObject params=new JSONObject();
		JSONObject result=new JSONObject();
		params.put("wdzrid", wdzrid);
		params.put("tzrxxid", tzrxxid);
		params.put("zrly", zrly);
		params.put("shr", shr);
		params.put("shzt", shzt);
		params.put("shbz", shbz);
		params.put("mjq", mjq);
		params.put("sxfbl", sxfbl);
		result.put("success", true);
		boolean isValid=p2pWdzrService.isValid(params);
		if (!isValid&&"01".equals(shzt)) {
			result.put("isSHSuc", false);
			result.put("msg", "信息有误,请刷新后重新操作!");
			serialize(request, response, result);
			return null;
		}
		boolean bool=p2pWdzrService.saveWdzrSh(params);
		result.put("isSHSuc", bool);
		serialize(request, response, result);
		return null;
	}
	@RequestMapping("/queryZrTzrxx")
	public ModelAndView queryZrTzrxxList(HttpServletRequest request,HttpServletResponse response)throws Exception{
		JSONObject result = new JSONObject();
		JSONObject params = new JSONObject();
		Integer start = RequestUtils.getIntParameter(request, "start");
		Integer limit = RequestUtils.getIntParameter(request, "limit");
		Integer page = RequestUtils.getIntParameter(request, "page");//新版本,传参
		String USERID=RequestUtils.getStringParameter(request, "USERID");
		String XMID=RequestUtils.getStringParameter(request, "XMID");
		String TZZT=RequestUtils.getStringParameter(request, "TZZT");
		String HKZT=RequestUtils.getStringParameter(request, "HKZT");
		String Q_HTBH=RequestUtils.getStringParameter(request, "Q_HTBH");
		if (null!=page) {//新版本,传参
			start=(page-1)*limit;
		}
		params.put("start", start);
		params.put("limit", limit);
		params.put("userid", USERID);
		params.put("xmid", XMID);
		params.put("tzzt", TZZT);
		params.put("hkzt", HKZT);
		params.put("htbh", Q_HTBH);
		List<Map<String, Object>> list=p2pWdzrService.queryZrTzrxxList(params);
		int count=p2pWdzrService.getZrTzrxxCount(params);
		float sumJe=p2pWdzrService.getZrTzrxxSum(params);
		result.put("data", list);
		result.put("total", count);
		result.put("sumJe", sumJe);
		if (null!=page) {//新版本,传参
			int totalPage=count%limit==0?count/limit:count/limit+1;
			result.put("totalPage", totalPage);
			//合计行 数据 开始
			Map<String, Object> map=new HashMap<>();
			map.put("USERID", "合计:");//key是字段名
			map.put("TZJE", sumJe);//key是字段名
			result.put("userdata", map);
			//合计行 数据 结束
		}
		serialize(request, response, result);
		return null;
	}
	@RequestMapping("/queryWdzrView")
	public ModelAndView queryWdzrView(HttpServletRequest request,HttpServletResponse response)throws Exception{
		JSONObject result = new JSONObject();
		JSONObject params = new JSONObject();
		Integer start = RequestUtils.getIntParameter(request, "start");
		Integer limit = RequestUtils.getIntParameter(request, "limit");
		String startDate = RequestUtils.getStringParameter(request, "startDate");
		String endDate = RequestUtils.getStringParameter(request, "endDate");
		String czrr=RequestUtils.getStringParameter(request, "czrr");
		String cxmid=RequestUtils.getStringParameter(request, "cxmid");
		String czrzt=RequestUtils.getStringParameter(request, "czrzt");
		params.put("start", start);
		params.put("limit", limit);
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		params.put("czrr", czrr);
		params.put("cxmid", cxmid);
		params.put("czrzt", czrzt);
		List<Map<String, Object>> list=p2pWdzrService.queryWdzrView(params);
		int count=p2pWdzrService.queryWdzrCount(params);
		result.put("data", list);
		result.put("total", count);
		serialize(request, response, result);
		return null;
	}
	/**
	 * @作者 js  
	 * @方法名 queryWdzrQxList  
	 * @功能 TODO 我的转让权限列表
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryWdzrQxList")
	public ModelAndView queryWdzrQxList(HttpServletRequest request,HttpServletResponse response)throws Exception{
		JSONObject result = new JSONObject();
		JSONObject params = new JSONObject();
		Integer start = RequestUtils.getIntParameter(request, "start");
		Integer limit = RequestUtils.getIntParameter(request, "limit");
		String yhm=RequestUtils.getStringParameter(request, "yhm");
		String qxzt=RequestUtils.getStringParameter(request, "qxzt");
		params.put("start", start);
		params.put("limit", limit);
		params.put("yhm", yhm);
		params.put("qxzt", qxzt);
		List<Map<String, Object>> list=p2pWdzrService.queryWdzrQxList(params);
		int count=p2pWdzrService.getWdzrQxCount(params);
		result.put("data", list);
		result.put("total", count);
		serialize(request, response, result);
		return null;
	}
	/**
	 * @作者 js  
	 * @方法名 saveWdzrQx  
	 * @功能 TODO 保存我的转让权限信息 授权
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/saveWdzrQx")
	public ModelAndView saveWdzrQx(HttpServletRequest request,HttpServletResponse response)throws Exception{
		JSONObject result = new JSONObject();
		JSONObject params = new JSONObject();
		String yhm=RequestUtils.getStringParameter(request, "yhm");
		String sqsx=RequestUtils.getStringParameter(request, "sqsx");
		String sqr=RequestUtils.getStringParameter(request, "sqr");
		params.put("yhm", yhm);
		params.put("sqsx", sqsx);
		params.put("sqr", sqr);
		result=p2pWdzrService.saveWdzrQx(params);
		serialize(request, response, result);
		return null;
	}
	@RequestMapping("/deleteWdzrQx")
	public ModelAndView deleteWdzrQx(HttpServletRequest request,HttpServletResponse response)throws Exception{
		JSONObject result = new JSONObject();
		String username=RequestUtils.getStringParameter(request, "yhm");
		boolean isDel=p2pWdzrService.deleteWdzrQx(username);
		result.put("success", true);
		if (isDel) {
			result.put("isSuc", true);
			result.put("msg", "收回权限成功");
		}else {
			result.put("isSuc", false);
			result.put("msg", "收回权限失败");
		}
		serialize(request, response, result);
		return null;
	}
}
